package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.VidsMapper;
import com.hqyj.pojo.Vids;
import com.hqyj.service.IVidsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-21
 */
@Service
public class VidsServiceImpl implements IVidsService {

    @Autowired(required = false)
    VidsMapper vidsMapper;
    @Override
    public HashMap<String, Object> queryList(Vids vids) {
        HashMap<String,Object> map = new HashMap<>();
        //定义分页对象
        Page<Vids> page = new Page<>(vids.getPage(),vids.getRow());
        //定义查询对象
        QueryWrapper<Vids> wrapper = new QueryWrapper();
        wrapper.eq(vids.getVidsId()!=null,"vids_id",vids.getVidsId());
        wrapper.like(vids.getVidsPhon()!=null&&!"".equals(vids.getVidsPhon()),"vids_phon",vids.getVidsPhon());

        //查询数据库
        Page<Vids> vidsPage=vidsMapper.selectPage(page,wrapper);

        //当前页集合
        map.put("list",vidsPage.getRecords());
        //总页数
        map.put("totalPage",vidsPage.getPages());
        //总条数
        map.put("total",vidsPage.getTotal());
        //当前页
        map.put("curPage",vids.getPage());
        if(vids.getPage()<=1){
            //上一页
            if (vids.getPage()==0){
                map.put("prePage",0);
            }else {
                map.put("prePage",1);
            }
        }else{
            map.put("prePage",vids.getPage()-1);

        }
        if(vids.getPage()>=vidsPage.getPages()){
            //下一页
            map.put("nextPage",vidsPage.getPages());

        }else{
            map.put("nextPage",vids.getPage()+1);

        }
        //每页显示条数
        map.put("row",vids.getRow());

        return map;

    }

    @Override
    public HashMap<String, Object> add(Vids vids) {
        HashMap<String,Object> map = new HashMap<>();

        int insert = vidsMapper.insert(vids);
        if(insert>0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Vids vids) {
        HashMap<String,Object> map = new HashMap<>();
        //创建查询对象
        QueryWrapper<Vids> wrapper = new QueryWrapper<>();
        wrapper.eq("vids_id",vids.getVidsId());
        int update = vidsMapper.update(vids, wrapper);
        if(update>0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }

        return map;
    }

    @Override
    public HashMap<String, Object> del(Vids vids) {
        HashMap<String,Object> map = new HashMap<>();
        int del = vidsMapper.deleteById(vids.getVidsId());
        if(del>0){
            map.put("info","删除成功");
        }else {
            map.put("info","删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String,Object> queryLv(String tel) {
        HashMap<String,Object> map = new HashMap<>();
        QueryWrapper<Vids> wrapper = new QueryWrapper<>();
        wrapper.eq(tel!=null&&!tel.equals(""),"vids_phon",tel);
        Vids vids = vidsMapper.selectOne(wrapper);
        if(vids!=null){
            map.put("data","存在");
            map.put("vids",vids);
            int vidNum=vids.getVidsNum();
            if(vidNum>=0&&vidNum<3000){
                map.put("lv","青铜");
                map.put("discount",0.95);
            }else if (vidNum<7000){
                map.put("lv","白银");
                map.put("discount",0.9);
            }else if(vidNum<12000){
                map.put("lv","黄金");
                map.put("discount",0.85);
            }else {
                map.put("lv","钻石");
                map.put("discount",0.8);
            }
        }else {
            map.put("data","不存在");
        }
        return map;
    }


}
